package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Admin;
import com.sansi.pencilbook.mapper.AdminMapper;
import com.sansi.pencilbook.service.AdminServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 管理员服务
 *
 * @author lsy13
 */
@Service
public class AdminServerImpl implements AdminServer {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result login(String account, String password) {
        Admin admin = (Admin) getAdminByAccount(account).getData();
        if (admin != null) {
            return admin.getAdminPassword().equals(password) ? Result.ofSuccess("登录成功") :
                    Result.ofFailed("登录失败, 密码错误");
        }else {
            return Result.ofFailed("登录失败, 账户错误");
        }
    }

    @Override
    public Result getAllAdmin() {
        try {
            List<Admin> list = adminMapper.findAll();
            return Result.ofSuccess("查询成功", list.size(), list);
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getAdminByAccount(String account) {
        try {
            Admin admin = adminMapper.findAdminByAccount(account);
            return admin!=null ? Result.ofSuccess("查询成功", 1, admin) :
                Result.ofFailed("查询查询不到该账户");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result modifyAdmin(Admin admin) {
        try {
            return Result.ofSuccess("修改管理员信息成功", 1, adminMapper.updateAdmin(admin));
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("修改管理员信息失败");
        }
    }

    @Override
    public Result saveAdmin(Admin admin) {
        try {
            return Result.ofSuccess("增加管理员成功", 1, adminMapper.saveAdmin(admin));
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("增加管理员失败");
        }
    }
}
